package top.jidanmian.iot.mqtt;

import org.springframework.web.bind.annotation.*;
import top.jidanmian.iot.mqtt.persistence.MqttReceivedMessage;

@RestController
public class MqttController {
    private final MqttService service;
    public MqttController(MqttService service) {
        this.service = service;
    }

    @PostMapping("/mqtt/sub")
    String subscribe(@RequestBody MqttRequestBody body) {
        service.subscribe(body.topic());
        return "subscribe - Topic: " + body.topic();
    }

    @PostMapping("/mqtt/pub")
    String publish(@RequestBody MqttRequestBody body) {
        service.sendMessage(body.topic(), body.message());
        return "Publish - Topic: " + body.topic() + ", message: " + body.message();
    }

    @GetMapping("/mqtt/message")
    MqttReceivedMessage getMessage(@RequestBody MqttRequestBody body) {
        return service.getMessage(body.topic());
    }
}
